package cn.xiaobai.service;

import cn.hutool.core.util.NumberUtil;
import cn.xiaobai.domain.Message;
import cn.xiaobai.enums.SimpleRegeditEnum;
import cn.xioabai.common.utility.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Objects;

/**
 * @Author yangdaji
 * @Date 2023/4/27 0027
 * @PackageName cn.xiaobai.service
 * @ClassName RegeditApplication
 */
@Component
public class RegeditApplication extends AbstractApplication {


    /**
     * 优先级
     */
    private final Integer priority = 2;

    @Autowired
    private RegeditCommandService regeditCommandService;


    @PostConstruct
    private void init() {
        List<Message> messageList = priorityMessageMap.get(priority);
        messageList.add(new Message().setNumber(0).setMessage("返回上一层"));
        messageList.add(new Message().setNumber(1).setMessage("添加功能到右键"));
        messageList.add(new Message().setNumber(2).setMessage("删除右键的功能"));
        messageList.add(new Message().setNumber(3).setMessage("查询右键的功能"));
        priorityMessageMap.put(priority, messageList);
    }

    /**
     * 显示简单右键提示信息
     */
    @Override
    public void showMessage(Integer priority) {
        System.out.println();
        super.showMessage(priority);
        System.out.print("请输入选项:");
        listenerInput();
    }

    @Override
    public void listenerInput() {

        Integer anInt = null;
        List<Integer> messageNumber = this.getMessageNumber(priority);
        // 判断输入的序号是否存在
        String mess = "没有该选项,请重新输入:";
        while (true) {
            // 序号不存在, 重新输入
            String nextStr = sc.nextLine();
            if (StringUtils.isNoneBlank(nextStr) && NumberUtils.isNumber(nextStr)) {
                anInt = NumberUtil.parseInt(nextStr);
                if (messageNumber.contains(anInt)) {
                    break;
                }
            }
            System.out.print(mess);
        }

        SimpleRegeditEnum item = SimpleRegeditEnum.getItem(anInt);
        if (Objects.nonNull(item) && anInt != 0) {
            SimpleRegeditEnum.setRegeditCommandService(regeditCommandService);
            item.execute();
            showMessage(priority);
        }
    }

    /**
     * 获取信息序号列表
     *
     * @return
     */

    @Override
    List<Integer> getMessageNumber(Integer priority) {
        return super.getMessageNumber(priority);
    }


}
